<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;仓库管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;质检管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
      <el-form ref="form" :model="qualityForm">
        <el-form-item class="quality_formSubmit_search">
          <el-select v-model="qualityForm.name" placeholder="请选择产品名称">
            <el-option
                v-for="item in qualityOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="quality.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="date" label="日期" width="200px"></el-table-column>
        <el-table-column prop="name" label="产品名称" width="200px"></el-table-column>
        <el-table-column prop="dateId" label="批号" width="200px"></el-table-column>
        <el-table-column prop="yield" label="产量" width="200px"></el-table-column>
        <el-table-column prop="isCheck" label="合格与否" width="200px"></el-table-column>
        <el-table-column prop="remarks" label="备注" width="200px"></el-table-column>
        <el-table-column prop="qualifiedNumber" label="合格数" width="200px"></el-table-column>
        <el-table-column prop="nonConformitiesNumber" label="不合格数" width="200px"></el-table-column>
        <el-table-column prop="failureRate" label="不合格率" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="quality.length">
        </el-pagination>
      </div>
      <el-dialog
          width="80%"
          :modal-append-to-body="false"
          title="编辑质检表单"
          :visible.sync="dialogFormVisible">
        <el-form
            :model="qualityForm"
            :rules="rules"
            ref="qualityForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="日期:" prop="date">
                <el-date-picker
                    v-model="qualityForm.date"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品名称:" prop="name">
                <el-input v-model="qualityForm.name" placeholder="请输入产品名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="批号:" prop="dateId">
                <el-input v-model="qualityForm.dateId" placeholder="输入批号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产量:" prop="yield">
                <el-input v-model="qualityForm.yield" placeholder="输入产量"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="合格数:" prop="qualifiedNumber">
                <el-input v-model="qualityForm.qualifiedNumber" placeholder="输入合格数"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="不合格数:" prop="nonConformitiesNumber">
                <el-input v-model="qualityForm.nonConformitiesNumber" placeholder="输入不合格数"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item label="不合格率:" prop="failureRate">
                <el-input v-model="qualityForm.failureRate" placeholder="输入不合格率"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item style="float: right">
                <el-button type="primary" @click="compute" style="background-color: white;border-color: gray">
                  <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="合格与否:" prop="isCheck">
            <el-input v-model="qualityForm.isCheck" placeholder="合格与否"></el-input>
          </el-form-item>
          <el-form-item label="备注:" prop="remarks">
            <el-input v-model="qualityForm.remarks" placeholder="输入备注"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" type="warning" style="color:white;">取 消</el-button>
          <el-button type="primary" @click="submitEditForm()" style="color:white;">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      qualityOptions: [],
      quality: [],
      qualityForm: {
        id: '',
        date: '',
        name: '',
        dateId: '',
        yield: '',
        isCheck: '',
        remarks: '',
        qualifiedNumber: '',
        nonConformitiesNumber: '',
        failureRate: '',
      },
      currentPage: 1,
      pageSize: 10,
      rules: {
        date: [
          {required: true, message: "请选择日期", trigger: 'change'}
        ],
        name: [
          {required: true, message: "请输入产品名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        dateId: [
          {required: true, message: "请输入批号", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        yield: [
          {required: true, message: "请输入产量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        isCheck: [
          {required: true, message: "是否合格", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        remarks: [
          {required: true, message: "请输入备注", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        qualifiedNumber: [
          {required: true, message: "请输入合格数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        nonConformitiesNumber: [
          {required: true, message: "请输入不合格数", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        failureRate: [
          {required: true, message: "请计算不合格率", trigger: 'blur'},
        ],
      },

    }
  },
  methods: {
    compute() {
      if (this.qualityForm.qualifiedNumber) {
        let lowQualify = parseInt(this.qualityForm.yield) - parseInt(this.qualityForm.qualifiedNumber);
        this.$set(this.qualityForm, 'nonConformitiesNumber', lowQualify);

        let rate = 100 * lowQualify / (parseInt(this.qualityForm.yield));
        let num = rate.substring(0, rate.indexOf('.') + 3);
        this.$set(this.qualityForm, 'failureRate', num);
        if (num < 0.05) {
          this.$set(this.qualityForm, 'isCheck', '是');
          this.$set(this.qualityForm, 'remarks', '经过检验计算,产品合格,其中' + ((1 - num) * 100) + '%的产品达到了合格的要求');
        } else {
          this.$set(this.qualityForm, 'isCheck', '否');
          this.$set(this.qualityForm, 'remarks', '经过检验计算,产品不合格,其中只有' + ((1 - num) * 100) + '%的产品达到了合格的要求');
        }
      }
      if (this.qualityForm.nonConformitiesNumber) {
        let highQualify = parseInt(this.qualityForm.yield) - parseInt(this.qualityForm.nonConformitiesNumber);
        this.$set(this.qualityForm, 'qualifiedNumber', highQualify);

        let rate = parseInt(this.qualityForm.nonConformitiesNumber) / (parseInt(this.qualityForm.yield));
        let num = rate.substring(0, rate.indexOf('.') + 3);
        this.$set(this.qualityForm, 'failureRate', num);
        if (num < 0.05) {
          this.$set(this.qualityForm, 'isCheck', '是');
          this.$set(this.qualityForm, 'remarks', '经过检验计算,产品合格,其中' + ((1 - num) * 100) + '%的产品达到了合格的要求');
        } else {
          this.$set(this.qualityForm, 'isCheck', '否');
          this.$set(this.qualityForm, 'remarks', '经过检验计算,产品不合格,其中只有' + ((1 - num) * 100) + '%的产品达到了合格的要求');
        }
      }
    },
    openEditDialog(qualityForm) {
      let url = 'http://localhost:9080/wareHouseQuality/' + qualityForm.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.qualityForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadQualityList();
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9080/wareHouseQuality/' + this.qualityForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.qualityForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改固定资产成功！',
            type: 'success'
          });
          this.loadQualityList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadQualityList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openDeleteConfirm(qualityForm) {
      let message = '此操作将永久删除【' + qualityForm.name + '】商品，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(qualityForm);
      }).catch(() => {
      });
    },
    handleDelete(qualityForm) {
      let url = 'http://localhost:9080/wareHouseQuality/' + qualityForm.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除固定资产成功！',
            type: 'success'
          });
          local.reload();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadQualityList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadQualityList() {
      let url = 'http://localhost:9080/wareHouseQuality';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('message=' + responseBody.message);
        this.quality = responseBody.data;
        console.log('fixedAssets=' + responseBody.data);
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadQualityList();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.quality_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>